package com.jk.controller;

import com.jk.entity.Company;
import com.jk.entity.Message;
import com.jk.entity.Register;
import com.jk.service.LoginService;
import com.jk.util.UpLoad;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.RestTest.sendphone;


/**
 * @author 荣楠楠
 * 创建时间：2018/4/11 22:14
 * @Description: TODO
 */

@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private LoginService loginService;
    @Resource
    private MongoTemplate mongoTemplate;


    @DeleteMapping
    @RequestMapping("/deletereg/{rid}")
    public void  deletereg(@PathVariable String rid){

        System.out.println("删除请求"+"rid = [" + rid + "]");
        //return  null;
    }



   /* *
    * @Description: $企业注册上传图片
    * @author rnn
    * @date 2018/4/24 20:17
    * @param [Photo, request]
    * @return java.lang.String
    */
    @PostMapping
    @RequestMapping("/uploadphoto")
    public String uploadphoto(@RequestParam("photos") MultipartFile Photo,HttpServletRequest request){
        //给图片重命名   用的时间戳
        String newFileName = UpLoad.upload(Photo, request);
        System.out.println(newFileName);
        return newFileName;


    }




    /* *
     * @Description: 企业注册  手机号，上传图片
     * @author rnn
     * @date 2018/4/16 16:45
     * @param [company]
     * @return com.jk.entity.Message
     */
    @PostMapping
    @RequestMapping("/regCompany")
    public String regCompany(Company company, HttpServletRequest  request){

        //生成UUID
        UUID uuid = UUID.randomUUID();

        String strID = uuid.toString().replaceAll("-", "");

        company.setCompanyId(strID);

        String com =  loginService.regCompany(company);

        return  com;
    }


   /* *
    * @Description:判断验证码是否正确
    * @author rnn
    * @date 2018/4/16 14:23
    * @param [meg, session]
    * @return com.jk.entity.Message
    */
      @PostMapping
      @RequestMapping("/getYzm")
      public  String getYzm(String meg,HttpServletRequest request){
         //验证码
          //String   yz = (String) session.getAttribute("yzm");

          HttpSession session = request.getSession();

          String yzm = (String) session.getAttribute("yzm");

         // Map<String,String> map=new HashMap();

         // map.get(yzm);

          System.out.println("发出的验证码"+yzm);

         // Message message = new Message();
          //输入验证码是否与发出验证码一致
          if (yzm.equals(meg)){

            /*  message.setMid(00);

              message.setMessage("登录成功");

              return message;*/
              System.out.println("对比后的验证码"+meg);
              return  "登录成功";

          }else{

           /*   message.setMid(02);

              message.setMessage("验证码不正确");

              return message;*/
              return  "验证码不正确";

          }

      }




    /* *
     * @Description:企业登录,验证手机号，发送验证码
     * @author rnn
     * @date 2018/4/12 23:25
     * @param [company]
     * @return com.jk.entity.Message
     */
    @PostMapping
    @RequestMapping("/queryCompany")
    public String loginCompany(String companyPersonPhone,HttpServletRequest request){

       // Message com =  loginService.loginCompany(company);

       //查询手机号是否存在
        Company pany =  loginService.queryCompanyPersonPhone(companyPersonPhone);

        Message message = new Message();

        if (pany==null){

            return  "手机号不正确";

        }

       else {

            //手机号存在时发送短信   四位随机数
            String suiji = (int) (Math.random() * 9000) + 1000+"";

           // System.out.println(suiji);

            try {


                 /* Map<String,String> map=new HashMap();

                  map.put("yz",suiji);

                  Yzm yy = new Yzm();

                  yy.setYzm(suiji);

                  yy.setMap(map);

                  mongoTemplate.insert(yy,"i_yzm");*/

                //调用短信接口
                sendphone(companyPersonPhone,suiji);


            } catch (IOException e) {
                e.printStackTrace();
            }

        }



        return "发送验证码";

    // return "登录成功";
    }


    /* *
     * @Description: 短信接口
     * @author rnn
     * @date 2018/4/12 18:55
     * @param [phone, request]
     * @return java.lang.String
     */
    @GetMapping("/sendmessage")
    @ResponseBody
    public Message note( String phone, HttpSession session){

        Message message = new Message();

        String regularExpression = "(^1[3|4|5|7|8]\\d{9}$)";

        boolean matche =phone.matches(regularExpression);

        //如果手机号不为空
        if (!matche) {

            //  验证码   四位随机数
            String suiji = (int) (Math.random() * 9000) + 1000 + "";

            System.out.println(suiji);

            session.setAttribute("yzm", suiji);

            try {
                //调用短信接口
                sendphone(phone, suiji);

            } catch (IOException e) {
                e.printStackTrace();
            }

            message.setMid(991);

            message.setMessage("手机号格式不正确");

            return message;

        }
            message.setMid(999);

            message.setMessage("发送验证码");

            return message;

    }


    /* *
     * @Description: 个人注册
     * @author rnn
     * @date 2018/4/12 11:44
     * @param [register]
     * @return com.jk.entity.Message
     */
   @PostMapping
   @RequestMapping("/saveRegister")
   public String saveRegister( Register register){

       //生成uuid
       UUID uuid = UUID.randomUUID();

       String strID = uuid.toString().replaceAll("-", "");

       register.setRegisterId(strID);

      // String  yzm = (String) session.getAttribute("yzm");

       Message registerSave = loginService.saveRegister(register);

       String message = registerSave.getMessage();

       return message;
   }

    /* *
     * @Description: 个人登录
     * @author rnn
     * @date 2018/4/12 10:36
     * @param [register, session]
     * @return com.jk.entity.Message
     */
    //@ResponseBody
    @PostMapping
    @RequestMapping("/queryRegister")
    public Message Login(Register register){

        Message queryRegister = loginService.Login(register);

        System.out.println("queryRegister = [" + queryRegister + "]");
         return queryRegister;

    }





}
